﻿Imports SecurityService
Imports System.Data
Imports VSModel
Imports Cls_Common

Partial Class Control_KhachSan_Edit
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            If Not Request.QueryString("Id").ToString.Equals("0") Then
                BindHotelType()
                hidID.Value = Request("Id")
                BindData()
            End If
        End If
    End Sub
    Protected Sub BindHotelType()
        Using _data As New VSEntities
            'huyen
            Dim lstHuyen = (From q In _data.Provinces Where q.ParentId = 232 Order By q.Title Select q.Id, q.Code, q.Title).ToList
            With ddlDistrict
                .DataSource = lstHuyen
                .DataTextField = "Title"
                .DataValueField = "Code"
                .DataBind()
                Dim itm As New ListItem("--- Chọn ---", "")
                .Items.Insert(0, itm)
            End With

            Dim p = (From q In _data.HotelTypes Select q).ToList
            With ddlHoteltypeid
                .DataTextField = "Title"
                .DataValueField = "HotelTypeId"
                .DataSource = p
                .DataBind()
                Dim itm As New ListItem("--- Chọn ---", "")
                .Items.Insert(0, itm)
            End With
        End Using
    End Sub
    Protected Sub BindData()
        Using data As New VSEntities
            Dim p As Hotel = (From q In data.Hotels Where q.Id = hidID.Value Select q).SingleOrDefault
            If Not p Is Nothing Then
                txtHotelCode.Text = IIf(IsNothing(p.HotelCode) = True, "", p.HotelCode)
                txtHotelname.Text = IIf(IsNothing(p.HotelName) = True, "", p.HotelName)
                ddlHoteltypeid.SelectedValue = p.HotelTypeId
                txtAddress.Text = IIf(IsNothing(p.Address) = True, "", p.Address)
                ddlDistrict.SelectedValue = IIf(IsNothing(p.District) = True, "", p.District)
                'huyen
                Dim _parentId As Integer = (From q In data.Provinces Where q.Code = ddlDistrict.SelectedValue Select q.Id).FirstOrDefault
                Dim lstPhuongXa = (From q In data.Provinces Where q.ParentId = _parentId Order By q.Title Select q.Id, q.Code, q.Title).ToList
                With ddlWard
                    .DataSource = lstPhuongXa
                    .DataTextField = "Title"
                    .DataValueField = "Code"
                    .DataBind()
                    Dim itm As New ListItem("--- Chọn ---", "")
                    .Items.Insert(0, itm)
                End With


                ddlWard.SelectedValue = IIf(IsNothing(p.Ward) = True, "", p.Ward)


                txtBusinesslicence.Text = IIf(IsNothing(p.BusinessLicence) = True, "", p.BusinessLicence)

                txtNumberofroom.Text = IIf(IsNothing(p.NumberOfRoom) = True, "", p.NumberOfRoom)
                txtNumberofstaff.Text = IIf(IsNothing(p.NumberOfStaff) = True, "", p.NumberOfStaff)
                txtDirector.Text = IIf(IsNothing(p.Director) = True, "", p.Director)
                txtPhone.Text = IIf(IsNothing(p.Phone) = True, "", p.Phone)
                txtFax.Text = IIf(IsNothing(p.Fax) = True, "", p.Fax)
                txtEmail.Text = IIf(IsNothing(p.Email) = True, "", p.Email)
                txtWebsite.Text = IIf(IsNothing(p.WebSite) = True, "", p.WebSite)
                hidUserId.Value = IIf(IsNothing(p.UserId) = True, "", p.UserId)
                txtUserName.Text = p.User.FullName & "-" & p.User.UserName

            End If
        End Using
    End Sub
#End Region


#Region "Event for control "
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Using data As New VSEntities

            Dim p As Hotel = (From q In data.Hotels Where q.Id = hidID.Value).SingleOrDefault
            If Not p Is Nothing Then
                'Dim k = (From q In data.Hotels Where q.HotelCode = txtHotelCode.Text And q.Id <> hidID.Value Select q).FirstOrDefault
                'If Not k Is Nothing Then
                '    Excute_Javascript("Mã khách sạn đã tồn tại trong hệ thống", Me.Page)
                '    Exit Sub
                'End If

                Try
                    p.HotelCode = txtHotelCode.Text.Trim()
                    p.HotelName = txtHotelname.Text.Trim()
                    p.HotelTypeId = ddlHoteltypeid.SelectedValue()
                    p.Address = txtAddress.Text.Trim()
                    p.Ward = ddlWard.SelectedValue
                    p.District = ddlDistrict.SelectedValue
                    p.BusinessLicence = txtBusinesslicence.Text.Trim()

                    Dim _NumberOfRoom As Integer
                    Try
                        _NumberOfRoom = CInt(txtNumberofroom.Text.Trim())
                    Catch ex As Exception
                        _NumberOfRoom = 0
                    End Try

                    Dim _NumberOfStaff As Integer
                    Try
                        _NumberOfStaff = CInt(txtNumberofstaff.Text.Trim())
                    Catch ex As Exception
                        _NumberOfStaff = 0
                    End Try

                    p.NumberOfRoom = _NumberOfRoom
                    p.NumberOfStaff = _NumberOfStaff

                    p.Director = txtDirector.Text.Trim()
                    p.Phone = txtPhone.Text.Trim()
                    p.Fax = txtFax.Text.Trim()
                    p.Email = txtEmail.Text.Trim()
                    p.WebSite = txtWebsite.Text.Trim()
                    p.UserId = hidUserId.Value()
                    Dim _Modifier As String = Session("UserName")
                    p.Modifier = _Modifier
                    p.Modified = Now
                    data.SaveChanges()
                    Insert_App_Log("Cập nhật Khách sạn:" & txtHotelname.Text.Trim & "", Function_Name.Hotel, Audit_Type.Edit, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))

                    Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.');window.location ='../../Page/Hotel/List.aspx';", Me.Page, True)
                Catch ex As Exception
                    log4net.Config.XmlConfigurator.Configure()
                    log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                    Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
                End Try
            End If
        End Using
    End Sub
    Protected Sub btnHuy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnHuy.Click
        Response.Redirect("List.aspx")
    End Sub
#End Region

    Protected Sub hplUser_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles hplUser.Init
        hplUser.CssClass = "thickbox"
        hplUser.NavigateUrl = "~/Page/Users/UserSelect.aspx?keepThis=true&TB_iframe=true&height=450&width=500&modal=true"
    End Sub
End Class


